<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 函数防抖：时间n秒后触发，不管在这个时间段中执行多少次时间，该事件只针对最后一次触发 -->
    <button id="btn">函数防抖</button>
    <script>
        var timer;
        var btn = document.getElementById("btn");
        btn.onclick = function(){
            if(timer){
                clearTimeout(timer)
            }
           timer =  setTimeout(()=>{
                console.log("loading");
                timer = null;
            },500)
        }
    </script>
</body>
</html>